package cn.exempale.mscapplication.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.exempale.mscapplication.R;
import cn.exempale.mscapplication.databinding.ItemResultLeftBinding;
import cn.exempale.mscapplication.databinding.ItemResultRightBinding;
import cn.exempale.mscapplication.model.db.entity.ResultEntity;

public class ResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ResultEntity> results;
    private String contact1;
    private String contact2;

    private static final int LEFT_TYPE = 0;
    private static final int RIGHT_TYPE = 1;

    public ResultAdapter(Context context) {
        this.context = context;
        this.results = new ArrayList<>();
    }

    public void setResults(List<ResultEntity> results) {
        this.results = results;
        notifyDataSetChanged();
    }

    public void setContact1(String contact1) {
        this.contact1 = contact1;
    }

    public void setContact2(String contact2) {
        this.contact2 = contact2;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (viewType == LEFT_TYPE) {
            ItemResultLeftBinding binding = DataBindingUtil.inflate(inflater,
                    R.layout.item_result_left, parent, false);
            viewHolder = new LeftViewHolder(binding);
        } else {
            ItemResultRightBinding binding = DataBindingUtil.inflate(inflater,
                    R.layout.item_result_right, parent, false);
            viewHolder = new RightViewHolder(binding);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof LeftViewHolder) {
            ((LeftViewHolder) holder).binding.setResult(results.get(position));
        } else if (holder instanceof RightViewHolder) {
            ((RightViewHolder) holder).binding.setResult(results.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return results == null ? 0 : results.size();
    }

    @Override
    public int getItemViewType(int position) {
        ResultEntity resultEntity = results.get(position);
        if (resultEntity.getSpeaker().equals(contact1))
            return LEFT_TYPE;
        else if (resultEntity.getSpeaker().equals(contact2))
            return RIGHT_TYPE;
        return LEFT_TYPE;
    }

    class LeftViewHolder extends RecyclerView.ViewHolder {

        private ItemResultLeftBinding binding;

        public LeftViewHolder(@NonNull ItemResultLeftBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    class RightViewHolder extends RecyclerView.ViewHolder {

        private ItemResultRightBinding binding;

        public RightViewHolder(@NonNull ItemResultRightBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
